package org.travel_system.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.travel_system.pojo.Spot;
import org.travel_system.service.SpotService;
import org.travel_system.utils.Result;

import java.util.List;

@RestController
@RequestMapping("/spots")
public class SpotController
{
    private final SpotService spotService;

    @Autowired
    public SpotController(SpotService spotService)
    {
        this.spotService = spotService;
    }

    @GetMapping
    public Result<List<Spot>> getSpots()
    {
        return Result.success(spotService.getSpots());
    }

    @GetMapping("/{name}")
    public Result<?> getSpotByName(@PathVariable String name)
    {
        Spot spot = spotService.getSpotByName(name);
        return spot == null ?
                Result.error("不存在该名称对应的景点") :
                Result.success(spotService);
    }

    @PostMapping
    public Result<?> addSpot(@RequestBody Spot spot)
    {
        return spotService.addSpot(spot) > 0 ?
                Result.success() :
                Result.error("增加景区信息失败");
    }

    @DeleteMapping
    public Result<?> deleteSpot(@RequestParam String name)
    {
        return spotService.deleteSpotByName(name) > 0 ?
                Result.success() :
                Result.error("删除景区信息失败");
    }

    @PutMapping
    public Result<?> updateSpot(@RequestBody Spot spot)
    {
        return spotService.updateSpot(spot) > 0 ?
                Result.success() :
                Result.error("更新景区信息失败");
    }
}
